package com.orangelife.mobile.address.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.sp.ShoppingSp;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.address.entity.AddressBean;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.zoning.activity.SelectZoningActicity;

import java.util.HashMap;
import java.util.Map;

public class AddAddressActivity extends OrangeLifeBaseActivity {

	private EditText username;
	private EditText phone;
	private TextView province;
	private EditText address;
	private LinearLayout ll;
	private TextView title;
	private String mprovince=null;
	private String mcity=null;
	private String mdistrict=null;
	private LinearLayout zoning=null;
	private Dialog dlgLoading;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_add);
		initView();
		findView();
	}

	private void initView(){
		username=(EditText)findViewById(R.id.et_username);
		phone=(EditText)findViewById(R.id.et_phone);
		province=(TextView)findViewById(R.id.et_province);
		address=(EditText)findViewById(R.id.et_address);
		ll=(LinearLayout)findViewById(R.id.ll_score);
		title=(TextView)findViewById(R.id.tv_title);
		zoning=(LinearLayout)findViewById(R.id.ll_zoning);
	}
	
	private void findView(){
		ll.setVisibility(View.INVISIBLE);
		title.setText("我的地址");
		zoning.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent();
				intent.setClass(AddAddressActivity.this, SelectZoningActicity.class);
				startActivityForResult(intent,1);
			}
		});
	}
	
	public void save(View view){
		
		if("".equals(username.getText().toString())){
			ToastHelper.getInstance()._toast("请填写联系人");
			return;
		}else if("".equals(phone.getText().toString())){
			ToastHelper.getInstance()._toast("请填写联系电话");
			return;
		}else if("".equals(mprovince)){
			ToastHelper.getInstance()._toast("请填选择收货城市");
			return;
		}else if("".equals(address.getText().toString())){
			ToastHelper.getInstance()._toast("请填收货地址");
			return;
		}else if(phone.length() != 11){
			ToastHelper.getInstance()._toast("联系电话格式错误");
			return;
		}else if(username.getText().toString().length()>6){
			ToastHelper.getInstance()._toast("姓名过长");
			return;
		}
		dlgLoading = DialogHelper.getInstance().createLoadingDialog(this,
				Constant.LOADING);
		dlgLoading.show();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("consignee", username.getText().toString());
		map.put("phone", phone.getText().toString());
		map.put("province",mprovince );
		map.put("city",mcity);
		map.put("district",mdistrict);
		map.put("address", address.getText().toString());
		map.put("isDefault", "1");
		JSONRequest.getInstance().sendRequest(JSONRequest.POST,Constant.ADDRESS_ADD_URL, map,handler,0, HttpUtil.ACTION_JSON);
	}
	@SuppressLint("HandlerLeak")
	Handler handler =new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 500:
				DialogHelper.closeDialog(dlgLoading);
				ToastHelper.getInstance()._toast(msg.obj.toString());
				break;
			case 1011:
				ToastHelper.getInstance()._toast(msg.obj.toString());
				isLogin(dlgLoading);
				break;
			case 0:
				if (msg.obj != null) {
					HashMap<String, Object> map = (HashMap<String, Object>) JSONHelper
							.jsonToMap(String.valueOf(msg.obj));
					if (map != null) {
						int errCode = Integer.parseInt(map.get(
								Constant.RESULT_KEY).toString());
						String errInfo = String.valueOf(map
								.get(Constant.RESULT_MESSAGE_KEY));
						if (errCode == Constant.RESULT_SUCCESS) {
							ShoppingSp.clearTradeAddressInfo();
							setResult(1);
							finish();
						} else {
							ToastHelper.getInstance()._toast(errInfo);
						}
					}
				}

				break;
			default:
				break;
			}
		}
		
	};
	
	public void openProvince(View view){
		Intent intent=new Intent();
		intent.setClass(AddAddressActivity.this, SelectZoningActicity.class);
		startActivityForResult(intent,1);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:
			if(data!=null){
				Map<String,Object> map = (Map<String, Object>) data.getSerializableExtra("zoning");
				mprovince=map.get("province").toString();
				mcity=map.get("city").toString();
				mdistrict=map.get("district").toString();
				province.setText(mprovince+mcity+mdistrict);
			}
			break;

		default:
			break;
		}
	}
	
}
